<?php
/**
 * 后台模块管理，用于添加城市、专业等原始数据类
 * edit by gemj
 * 2011－03－23
 */

class EAdmin_Page_Ajax extends EAdmin_Page_Abstract
{
    public function validate(ME_Request $input, ME_Response $output)
    {
        $module_list = array();
        $module_arr = Libs_Admin_Module::getModuleList(array());
        if(!empty($module_arr) && is_array($module_arr)){
            foreach ($module_arr as $module){
                $id = $module['id'];
                $name = $module['name'];
                $module_list[$id] = $name;
            }
        }
        $output->module_list = $module_list;

        return true;
    }

    public function doDefault(ME_Request $input, ME_Response $output)
    {
        $catogoryadd = $input->post("catogoryadd");
        if(!empty ($catogoryadd)){
            $name = $input->post("name");
            $classid = $input->post("classid");
            $subclassid = $input->post("subclassid");
            $module_type = $input->post("module_type");
        }
        $list = Libs_Admin_Category::getCategoryList();
        $output->list = $list;

        $output->setTemplate("CategoryManage");
    }


    #获取类别
    public function doGetClass(ME_Request $input, ME_Response $output)
    {
        $module_type = $input->get("module_type");
        $list = Libs_Admin_Category::getCategoryList(array("module_type"=>$module_type,"type"=>1));
        $newlist = array();
        if(!empty($list) && is_array($list)){
            foreach ($list as $key=>$val){
                $classid = $val['classid'];
                $name = $val['name'];
                $newlist[$key]['id'] = $classid;
                $newlist[$key]['name'] = $name;
            }
        }
        
        echo json_encode($newlist);
        exit;
    }

    #获取子类
    public function doGetSubClass(ME_Request $input, ME_Response $output)
    {
        $module_type = $input->get("module_type");
        $classid = $input->get("classid");
        $list = Libs_Admin_Category::getCategoryList(array("module_type"=>$module_type,"classid"=>$classid,"type"=>2));
        $newlist = array();
        
        if(!empty($list) && is_array($list)){
            foreach ($list as $key=>$val){
                $subclassid = $val['subclassid'];
                $name = $val['name'];
                $newlist[$key]['id'] = $subclassid;
                $newlist[$key]['name'] = $name;
            }
        }

        echo json_encode($newlist);
        exit;
    }

   public function doChangeState(ME_Request $input, ME_Response $output)
   {
   	$eid = $input->get('eid');
   	$state = $input->get("stateid");
   	$idstr = $input->get("idstr");

	$db = Db_Resume::instance();

	if($state){
		$id_str = str_replace("_",",",$idstr);
		$sql = "update e_resume_job_relation set state={$state} where uid in({$id_str}) and firm_id={$eid}";
		$re = $db->query($sql);

		if($re){
			echo "1";
			exit;
		}
	}

	echo "0";exit;
   }

   public function doSetTags(ME_Request $input, ME_Response $output)
   {
	$eid = $input->get('eid');
	$tagid = $input->get("tagid");
        $idstr = $input->get("idstr");

        $db = Db_Resume::instance();
	if($tagid){
		$id_str = str_replace("_",",",$idstr);
		$sql = "select uid from e_resume_tags where uid in({$id_str}) and eid={$eid} and tagid={$tagid}";
		$taglist = $db->getAll($sql);
		$has_tag_arr = array();
		if($taglist){
			foreach($taglist as $tag){
				array_push($has_tag_arr,$tag['uid']);
			}
		}
		
		$id_arr = explode("_",$idstr);
		$needid_arr = array();
		if($has_tag_arr){
			$needid_arr = array_diff($id_arr,$has_tag_arr);
		}else{
			$needid_arr = $id_arr;
		}
		if($needid_arr){
			foreach($needid_arr as $userid){
				$sql = "insert into e_resume_tags(uid, eid, tagid) values($userid, $eid, $tagid)";
				$db->query($sql);
			}
		}
	}

	echo "1";exit;
   }

   public function doSetPlace(ME_Request $input, ME_Response $output)
  {
	$eid = $input->get('eid');
	$idstr = $input->get('idstr');
  	$placetype = $input->get('placetype');
	$placelist = $input->get('placelist');
	$placetime = $input->get('placetime');

	if(empty($eid) || empty($idstr) || empty($placetype) || empty($placelist) || empty($placetime)){
		return false;
	}

        $db = Db_Resume::instance();
	
	$id_arr = explode("_",$idstr);
	if($id_arr){
		foreach($id_arr as $uid){
			$sql = 'insert into e_place_plan(uid, eid, type, placeid, ptime) values('.$uid.', '.$eid.', '.$placetype.', '.$placelist.', "'.$placetime.'")';
			$db->query($sql);
		}
	}

	echo "1";	
	exit;
  }

  public function doTableFields(ME_Request $input, ME_Response $output)
  {
  	$table_name = $input->get('val');

	$db = Db_Resume::instance();

	$fields_arr = array();
	if($table_name){
		$sql = "SHOW COLUMNS FROM {$table_name}";
		$fields = $db->getAll($sql);
		foreach($fields as $key=>$field){
			$field = array_values($field);
			$fields_arr[$key]['Value'] = $field[0];
			$fields_arr[$key]['Text'] = $field[0];
		}
	}
	print_r(json_encode($fields_arr));
	exit;
  }

}
